package com.fei.BYSJ.controller;

import com.alibaba.fastjson.JSON;
import com.fei.BYSJ.beans.Data;
import com.fei.BYSJ.dao.UserMapper;
import com.fei.BYSJ.entity.User;
import com.fei.BYSJ.utils.HttpServletRequestUtil;
import com.fei.BYSJ.utils.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/login")
public class LoginController {
    @Autowired
    UserMapper userMapper;

    @RequestMapping("/gologin")
    public String login(HttpServletRequest request, Model model, HttpSession session){
        Map<String,Object> params = HttpServletRequestUtil.getRequestParams(request);
        String userAccount = (String) params.get("userAccount");
        String userPassword = (String) params.get("userPassword");


        if (null==userAccount||userPassword==""){
            model.addAttribute("msg","用户名或密码不能为空");

            return "login";
        }
        List<User> user   =  userMapper.findByLogin(userAccount,userPassword);
        if (user.size()>0){
            User u = user.get(0);
            System.out.println(JSON.toJSON(u));
            session.setAttribute("user",u);
            if(u.getUsertype()==1){
                return "redirect:/order/orderlist";
            }else if (u.getUsertype()==0){
                return "redirect:/page/manager/orderManager";
            }
       return "login";

        }else {
            model.addAttribute("msg","用户名或密码错误");
            return "login";
        }




    }
    @RequestMapping("/goregister")
 public @ResponseBody Data  goregister(HttpServletRequest request,HttpSession session){
        Map<String,Object> params = HttpServletRequestUtil.getRequestParams(request);
        User user = new User();
        user.setUseraccount((String) params.get("userAccount"));
        user.setUseraddress((String) params.get("userAddress"));
        user.setUsername((String) params.get("userName"));
        user.setUserpassword((String) params.get("userPassword"));
        user.setUsersign((String) params.get("userSign"));
        user.setUsertype(1);
        user.setUsersex((String) params.get("userSex"));
        user.setUserphone((String) params.get("userPhone"));

        int i = userMapper.insert(user);
        if (i==1){
            User u  = userMapper.findByAccount((String) params.get("userAccount"));
            session.setAttribute("user",u);
            return ResultData.success("注册成功","/order/orderlist");
        }else {
            return ResultData.failed("注册失败","");
        }





    }

    @RequestMapping("/checkAccount")
    public @ResponseBody
    Data checkAccount(HttpServletRequest request){
        Map<String,Object> params = HttpServletRequestUtil.getRequestParams(request);


        User user =userMapper.findByAccount((String) params.get("userAccount"));
        System.out.println(JSON.toJSON(user));
        if (null==user){

            return ResultData.success("账号未被注册","");


        }else {

            return ResultData.failed("账号已存在","");
        }




    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "login";
    }


}
